<?php

namespace Home\Controller;

use Think\Controller;

class CustomerController extends Controller
{
    /**
     * 会员中心
     */
    public function index()
    {
        $orders = M('Orders');
        $customer_id = $_SESSION['customer']['id'];
        //查出当前用户的信息
        $customer_info = M('Customer')->where("id = '$customer_id'")->find();
        //待付款
        $daifu = $orders->where("status = 1 and customer_id = '$customer_id'")->count();
        //待发货
        $daifa = $orders->where("status = 2 and customer_id = '$customer_id'")->count();
        //已发货
        $yifa = $orders->where("status = 3 and customer_id = '$customer_id'")->count();
        //已完成
        $wancheng = $orders->where("status = 1 and customer_id = '$customer_id'")->count();

        $this->assign(compact('customer_info', 'daifu', 'daifa', 'yifa', 'wancheng'));
        $this->display();
    }

    /**
     * 前端---注册页面
     */
    public function ajax_register()
    {
        $Customer = M('Customer');
        if (IS_AJAX) {
            $data['nickname'] = trim(I("post.nickname"));
            $data['mobile'] = trim(I("post.mobile"));
            $data['email'] = trim(I("post.email"));
            $data['sex'] = trim(I("post.sex"));
            $data['thumb'] = I("post.thumb");
            $data['birthday'] = trim(I("post.birthday"));

            //将地址分别存到不同的字段里
            $area = explode(" ", I('post.area'));
            $data['province'] = $area[0];
            $data['city'] = $area[1];
            $data['detail'] = $area[2];

            $data['password'] = set_password(I("post.password"));
            $check_password = I("post.check_password");
            $data['created_at'] = time();

            //生成会员号
            $data['customer_no'] = make_on();

            $preg_email = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/";
            $preg_mobile = "/^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$/";

            $info = array('status' => 1, 'msg' => '注册成功');

            //判断用户名的长度
            if (strlen($data['nickname']) < 5 || strlen($data['nickname']) > 20) {
                $info = array('status' => 0, 'msg' => '用户名的长度必须在5-20之间');
            }
            //判断手机号格式
            if (!preg_match($preg_mobile, $data['mobile'])) {
                $info = array('status' => 0, 'msg' => '手机号格式不正确');
            }
            //判断邮箱号格式
            if (!preg_match($preg_email, $data['email'])) {
                $info = array('status' => 0, 'msg' => '邮箱格式不正确');
            }
            //判断性别不能为空
            if (strlen($data['sex']) == "") {
                $info = array('status' => 0, 'msg' => '性别不能为空');
            }
            if (strlen($data['birthday']) == "") {
                $info = array('status' => 0, 'msg' => '生日不能为空');
            }
            //判断密码的长度
            if (strlen(I('post.password')) < 6) {
                $info = array('status' => 0, 'msg' => '密码的长度不能小于6位');
            }
            //判断两次输入的密码是否一致
            if ($check_password != I('post.password')) {
                $info = array('status' => 0, 'msg' => '两次输入的密码不一致');
            }
            //判断用户名/手机号/邮箱/是否唯一
            $nickname = $Customer->where("nickname = '$data[nickname]'")->find();
            $mobile = $Customer->where("nickname = '$data[mobile]'")->find();
            $email = $Customer->where("nickname = '$data[email]'")->find();

            if ($nickname || $mobile || $email) {
                $info = array('status' => 0, 'msg' => '用户名/手机号/邮箱已经存在');
            }
            if ($info['status'] == 1) {
                $Customer->add($data);
            }
            $this->ajaxReturn($info);
        } else {
            $this->display();
        }
    }

    /**
     * 前端---登录页面
     */
    public function login()
    {
        $Customer = M('Customer');
        if (IS_AJAX) {
            $data['nickname|mobile|email'] = trim(I('post.nickname'));
            $data['password'] = set_password(I("post.password"));

            $token = make_token(); //生成token
            $Customer->where($data)->setField('token', $token);

            $customer = $Customer->where($data)->find();
            if (!$customer) {
                $info = array('status' => 0, 'msg' => '用户名或密码错误');
            } else {
                $info = array('status' => 1, 'msg' => '登录成功');
                $_SESSION['customer'] = $customer;
            }

            $this->ajaxReturn($info);
        } else {
            $this->display();
        }
    }

    /**
     * 修改个人资料
     */
    public function info()
    {
        $customer_id = $_SESSION['customer']['id'];
        $customer = M('Customer')->where("id = '$customer_id'")->find();

        if (IS_POST) {
            $data['nickname'] = I('post.nickname');
            $data['thumb'] = I('post.thumb');
            $data['mobile'] = I('post.mobile');
            $data['email'] = I('post.email');
            $data['birthday'] = I('post.birthday');

            $area = explode(' ', I('post.area'));
            $data['province'] = $area[0];
            $data['city'] = $area[1];
            $data['detail'] = $area[2];

            $preg_email = "/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/";
            $preg_mobile = "/^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$/";

            //判断用户名的长度
            if (strlen($data['nickname']) < 5 || strlen($data['nickname']) > 20) {
                $this->error('用户名的长度必须在5-20之间');
            }
            //判断手机号格式
            if (!preg_match($preg_mobile, $data['mobile'])) {
                $this->error('手机号格式不正确');
            }
            //判断邮箱号格式
            if (!preg_match($preg_email, $data['email'])) {
                $this->error('邮箱格式不正确');
            }

            M('customer')->where("id = '$customer_id'")->save($data);
            $this->success('编辑个人信息成功');
        } else {
            $this->assign(compact('customer'));
            $this->display();
        }
    }


    /***
     * 编辑个性签名
     */
    public function editSign()
    {
        $customer_id = $_SESSION['customer']['id'];
        if (IS_AJAX) {
            $sign = trim(I('post.sign'));
            $result = M('customer')->where("id = '$customer_id'")->setField('sign', $sign);
            if ($result) {
                $info = array('status' => 1, 'msg' => '编辑成功');
            } else {
                $info = array('status' => 0, 'msg' => '编辑失败');
            }

            $this->ajaxReturn($info);
        }
    }

    /**
     * 会员中心---我的订单
     */
    public function orders()
    {
        $order = M('Orders');
        $customer_id = $_SESSION['customer']['id'];
        $ab = $order->where("customer_id= '$customer_id'")->find();

        $status = I('get.status');
        if ($status == "all") {
            $order_map = array('customer_id' => $customer_id);
        } else {
            $order_map = array('customer_id' => $customer_id, $ab['status'] => $status);
        }

        //查处用户对应的订单
        $all_orders = M('Orders')->where("customer_id = '$customer_id'")->select();
        foreach ($all_orders as &$order) {
            $order['order_product'] = M('Order_product')->where("order_id = '$order[id]'")->select();
            foreach ($order['order_product'] as &$prodcuts) {
                $prodcuts['product'] = M('Product')->where("id = '$prodcuts[product_id]'")->find();
            }
        }
//        p($prodcuts['product']);exit;
        $this->assign(compact('all_orders'));

        $this->display();
    }

    /**
     * 取消订单
     * 只有待发货的订单有取消订单
     */
    public function cancel_order()
    {
        if (IS_AJAX) {
            $id = I('post.id');
            $status = I('post.status');
            M('Orders')->where("id = '$id' and status = '$status'")->setField('status', 0);
        }
    }


    /**
     * 会员中心---物流信息
     */
    public function express()
    {
        $this->display();
    }


    /**
     * 会员中心---充值页面
     */
    public function inquiry()
    {
        $this->display();
    }


    /**
     * 安全退出登录
     */
    public function logout()
    {
        $_SESSION = array();
        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), "", time() - 3600, "/");
        }
        //彻底销毁session, 注意不要只使用这个函数, 它并不会删除服务器中的session数组和cookie, 所以需要结合以上使用.
        session_destroy();

        $this->redirect('Index/index');
//        $this->success('退出成功', U('Index/index'));
    }


}
